package com.zhenwei.zscm.common.utils;


import com.zhenwei.zscm.common.exception.CustomException;
import jakarta.annotation.Resource;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validator;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;

@Component
public class ValidateUtil {
    @Resource
    private Validator validator;

    public void validateList(List<?> list,Class<?> clazz) {
        StringBuilder sb = new StringBuilder();
        for (Object obj : list) {
            Set<ConstraintViolation<Object>> validations = validator.validate(obj, clazz);
            if(!validations.isEmpty()){
                for(ConstraintViolation<Object> violation : validations){
                    sb.append(violation.getMessage()).append(" ");
                }
            }
        }
        if(!sb.isEmpty()){
            throw new CustomException(sb.toString());
        }
    }
}
